class GameSystemZBT extends Behaviour {

    system = CollisionDetectionSystem;
    gameResult = false;

    onUpdate() {
        if (!this.gameResult) {
            this.gameResult = this.checkResult();
            if (this.gameResult) {
                console.log("win");
            }
        }
    }

    checkResult() {
        const parentButton = core.getObjectById("buttons");
        for (let child of parentButton.children) {
            if (!child.getBehaviour(ClickButtonZBT).isFound) {
                return false;
            }
        }
        return true;
    }
}

class GameInitSystemZBT extends Behaviour {

    image = null;
    data = null;

    onStart() {
        //console.log(this)
        this.getData();
        this.initGameObject();

    }

    getData() {
        this.image = gameProcess.image;
        this.data = gameProcess.gameData;
    }

    initGameObject() {
        const system = this.gameObject.getBehaviour(GameSystemZBT);
        const parentButton = core.getObjectById("buttons");
        const parentImage = core.getObjectById("images");

        const keys = Object.keys(this.data);
        for (let key of keys) {
            const obj = new GameObject();
            const transform = new Transform();
            obj.id = key;
            if (this.data[key].x) {
                transform.x = this.data[key].x;
            }
            if (this.data[key].y) {
                transform.y = this.data[key].y;
            }
            obj.addBehaviour(transform);
            switch (this.data[key].type) {
                case "image":
                    const image = new ImageRenderer();
                    image.image = this.data[key].image;
                    obj.addBehaviour(image);
                    parentImage.addChild(obj);
                    break;
                case "button":
                    const button = new ClickButtonZBT();
                    button.image = this.image.button;
                    obj.addBehaviour(button);
                    parentButton.addChild(obj);
                    break;
            }

        }
    }

}

class ClickButtonZBT extends Button {

    isFound = false;

    onStart() {
        this.onClick = () => {
            if (!this.isFound) {
                this.isFound = true;
                this.image = "./image/找不同/答案红框.png";
            }
        }
    }
}

class RestartButtonZBT extends Button {
    onStart() {
        this.onClick = () => {
            core.changeScene("./data/scenes/zhaobutong.json");
        }
    }
}

class EndgameButtonZBT extends Button {
    
    onStart() {
        this.onClick = () => {
            gameProcess.setGameResult(core.getObjectById("gameSystemZBT").getBehaviour(GameSystemZBT).gameResult);
            gameProcess.changeSceneBack("morework");
        }
    }
}

core.registerBehaviourClass(GameSystemZBT);
core.registerBehaviourClass(GameInitSystemZBT);
core.registerBehaviourClass(ClickButtonZBT);
core.registerBehaviourClass(RestartButtonZBT);
core.registerBehaviourClass(EndgameButtonZBT);